package com.joolun.mall.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joolun.mall.config.CommonConstants;
import com.joolun.mall.entity.OrderInfo;
import com.joolun.mall.entity.Work;
import com.joolun.mall.entity.WorkClick;
import com.joolun.mall.enums.OrderInfoEnum;
import com.joolun.mall.mapper.WorkMapper;
import com.joolun.mall.service.WorkClickService;
import com.joolun.mall.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service

public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements WorkService {

    @Autowired
    private  WorkClickService workClickService;

    private final SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public IPage<Work> page1(IPage<Work> page, Work entity) {

        IPage<Work> rs = baseMapper.selectPage(page, entity);

        List<Work> workList = rs.getRecords();
        workList.forEach(work -> {
            LocalDateTime createTime = work.getCreateTime();

            Date date = Date.from(createTime.atZone(ZoneId.systemDefault()).toInstant());

            // 发布日期 转换-》 1周前,1个月前,3个月前,半年前,1年前
            int days = daysBetween(sd.format(date));
            if (days >= 365) {
                work.setHowLongAgo("1年前");
            } else if (days >= 180) {
                work.setHowLongAgo("半年前");
            } else if (days >= 90) {
                work.setHowLongAgo("3个月前");
            } else if (days >= 30) {
                work.setHowLongAgo("1个月前");
            } else if (days >= 7) {
                work.setHowLongAgo("1周前");
            } else if(days >= 6) {
                work.setHowLongAgo("6天前");
            } else if(days >= 15) {
                work.setHowLongAgo("15天前");
            } else if(days >= 5) {
                work.setHowLongAgo("5天前");
            } else if(days >= 4) {
                work.setHowLongAgo("4天前");
            } else if(days >= 3) {
                work.setHowLongAgo("3天前");
            } else if(days >= 2) {
                work.setHowLongAgo("2天前");
            } else if(days >= 1) {
                work.setHowLongAgo("1天前");
            }else {
                work.setHowLongAgo("今天");
            }
            work.setCountClick(workClickService.count(Wrappers.query(new WorkClick()).lambda()
                    .eq(WorkClick::getWorkId, work.getId())));

        });
        return rs;
    }


    /**
     * 计算登记日期 距离当前时间天数
     *
     * @param startDate 登记日期
     * @return 天数
     * @throws Exception
     */
    public Integer daysBetween(String startDate) {

        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(this.sd.parse(startDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long time1 = cal.getTimeInMillis();

        cal = Calendar.getInstance();
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

}
